package com.ysd.twocrm.token;

import io.jsonwebtoken.*;
import org.bouncycastle.util.encoders.Base64;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Date;

public class JwtUtils {

    /*
    * 签发jwt
    * @param id
    * @param subject 可以是json数据
    * @param ttlMillis
    * @return String
    * */
       public static String createJWT(String id,String subject,long ttlMillis){
           SignatureAlgorithm signatureAlgorithm=SignatureAlgorithm.HS256;
           long nowMillis =System.currentTimeMillis();
           Date now =new Date(nowMillis);
           SecretKey secreKey=generalKey();
           JwtBuilder builder= Jwts.builder().setId(id)
                   .setSubject(subject)//主题
                   .setIssuer("刘苏朋")//签发者
                   .setIssuedAt(now)//签发时间
                   .signWith(signatureAlgorithm,secreKey);//签名算法以及密钥
           if(ttlMillis>=0){
                long expMillis=nowMillis+ttlMillis;
                Date expDate=new Date(expMillis);
                builder.setExpiration(expDate);//过期时间
           }
           return builder.compact();
       }


       public static SecretKey generalKey(){
            byte[] encodedKey = Base64.decode(SystemConstant.JWT_SECERT);
            SecretKey key=new SecretKeySpec(encodedKey,0,encodedKey.length,"ASE");
            return key;
       }

       /*
       * 验证jwt
       * */
       public static CheckResult validateJWT(String jwtStr){
             CheckResult checkresult=new CheckResult();
           Claims claims=null;

          try {
              claims=parseJWT(jwtStr);
              checkresult.setSuccess(true);
              checkresult.setClaims(claims);
          }catch (ExpiredJwtException e){
               checkresult.setErrCode(SystemConstant.JWT_ERRCODE_EXPIRE);
               checkresult.setSuccess(false);
          }catch (SignatureException e){
                checkresult.setErrCode(SystemConstant.JWT_ERRCODE_FAIL);
                checkresult.setSuccess(false);
          }catch (Exception e){
               checkresult.setErrCode(SystemConstant.JWT_ERRCODE_FAIL);
               checkresult.setSuccess(false);
          }

                     return checkresult;
       }

       /*
       * 解析JWT字符串
       * */
       public static Claims parseJWT(String jwt) {
            SecretKey secretKey=generalKey();
            return Jwts.parser().setSigningKey(secretKey).parseClaimsJws(jwt).getBody();
       }

  /*
   *   public static void main(String[] args) throws InterruptedException {
         String sc=createJWT("1","张三",60 * 60 * 1000);
         System.out.println(sc);

         CheckResult checkResult=validateJWT(sc);
         System.out.println(validateJWT(sc).getErrCode());
         System.out.println(validateJWT(sc).getClaims().getId());
        System.out.println(validateJWT(sc).getClaims());
         Thread.sleep(60 * 60 * 1000);
         System.out.println(new Date());
         System.out.println(sc);
         System.out.println(validateJWT(sc).getClaims());
    }
   * */
}
